// 报警管理 - 报警列表接口
const Router = require("koa-router");
const router = new Router();

// 报警数据结构
const alarmList = [
  {
    description: "充电桩拿不下来",
    address: "北京市东城区",
    equNo: "CD1001",
    level: 1, //1严重 2紧急 3一般
    time: "2024-09-15 09:33:24",
    code: 10023, //故障代码
    status: 1, //1待指派 2处理中 处理异常
  },
  {
    description: "电动车无法充电",
    address: "上海市浦东新区",
    equNo: "CD1002",
    level: 2,
    time: "2024-09-16 10:15:00",
    code: 10024,
    status: 2,
  },
  {
    description: "充电结束未通知",
    address: "广州市天河区",
    equNo: "CD1003",
    level: 3,
    time: "2024-09-17 11:28:45",
    code: 10025,
    status: 1,
  },
  {
    description: "设备显示屏故障",
    address: "深圳市南山区",
    equNo: "CD1004",
    level: 1,
    time: "2024-09-18 14:05:12",
    code: 10026,
    status: 1,
  },
  {
    description: "无法启动充电",
    address: "重庆市渝中区",
    equNo: "CD1005",
    level: 2,
    time: "2024-09-19 08:43:09",
    code: 10027,
    status: 2,
  },
  {
    description: "充电枪接触不良",
    address: "杭州市西湖区",
    equNo: "CD1006",
    level: 3,
    time: "2024-09-20 13:17:38",
    code: 10028,
    status: 3,
  },
  {
    description: "设备漏电报警",
    address: "成都市武侯区",
    equNo: "CD1007",
    level: 1,
    time: "2024-09-21 07:26:55",
    code: 10029,
    status: 2,
  },
];

/**
 * 报警列表接口
 * 路径：GET /alarm/alarmList
 *
 * 说明：此接口不需要前端传递任何参数，直接返回完整的报警数据列表
 * 用于报警管理模块展示所有报警信息
 */
router.get("/alarm/alarmList", (ctx) => {
  try {
    console.log("报警列表接口被调用，返回完整报警数据");

    // 返回完整报警数据
    ctx.body = {
      code: 200,
      success: true,
      message: "操作成功",
      data: alarmList,
    };
  } catch (error) {
    console.error("报警列表接口出错:", error);
    ctx.body = {
      code: 500,
      success: false,
      message: `服务器内部错误: ${error.message}`,
      data: null,
    };
  }
});

/**
 * 报警统计接口
 * 路径：GET /alarm/alarmStats
 *
 * 说明：此接口返回报警数据的统计信息，包括各级别报警数量和各状态报警数量
 */
router.get("/alarm/alarmStats", (ctx) => {
  try {
    console.log("报警统计接口被调用，返回报警统计数据");

    // 按级别统计
    const levelStats = {
      level1: alarmList.filter((item) => item.level === 1).length,
      level2: alarmList.filter((item) => item.level === 2).length,
      level3: alarmList.filter((item) => item.level === 3).length,
    };

    // 按状态统计
    const statusStats = {
      status1: alarmList.filter((item) => item.status === 1).length,
      status2: alarmList.filter((item) => item.status === 2).length,
      status3: alarmList.filter((item) => item.status === 3).length,
    };

    // 今日报警数量（模拟数据）
    const todayAlarms = alarmList.filter((item) =>
      item.time.includes("2024-09-24")
    ).length;

    // 返回统计信息
    ctx.body = {
      code: 200,
      success: true,
      message: "操作成功",
      data: {
        total: alarmList.length,
        levelStats,
        statusStats,
        todayAlarms,
      },
    };
  } catch (error) {
    console.error("报警统计接口出错:", error);
    ctx.body = {
      code: 500,
      success: false,
      message: `服务器内部错误: ${error.message}`,
      data: null,
    };
  }
});

// 导出路由
module.exports = router;
